﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Controls.Primitives;
namespace HA.ModFlow.Controls.UserControls
{
    /// <summary>
    /// winMultiInput.xaml 的交互逻辑
    /// </summary>
    public partial class winMultiInput : Window
    {
        public winMultiInput(List<double> data)
        {
            InitializeComponent();
            this._lstDouble = data;

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;
        }


        List<double> _lstDouble;

        public List<double> LstDouble
        {
            get { return _lstDouble; }
            set { _lstDouble = value; }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            GetData();
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dtGrid.CanUserAddRows = false;    
            ShowData(this._lstDouble);
            this.dtGrid.Columns[0].Width = 200;
            this.dtGrid.CanUserSortColumns = false;
        }
        private void ShowData(List<double> arr)
        {
            double[] arry = new double[arr.Count];
            BindingOperations.ClearAllBindings(this.dtGrid);
            this.dtGrid.ItemsSource = null;
            for (int i = 0; i < arr.Count; i++)
            {
                arry[i] = arr[i];
            }
            this.dtGrid.ItemsSource = ConvertToArry("data", arry).DefaultView;
            this.DataContext = arry;
        }
        /// <summary>
        /// 把一个一维数组转换为DataTable
        /// </summary>
        /// <param name="ColumnName">列名</param>
        /// <param name="Array">一维数组</param>
        /// <returns>返回DataTable</returns>
        /// <remarks> </remarks>
        public static DataTable ConvertToArry(string ColumnName, double[] Array)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(ColumnName, typeof(double));

            for (int i = 0; i < Array.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr[ColumnName] = Array[i].ToString();
                dt.Rows.Add(dr);
            }

            return dt;
        }
        private void GetData()
        {
            int i = 0;
            foreach (DataRowView dr in dtGrid.Items)
            {
                this._lstDouble[i]=Convert.ToDouble(dr[0].ToString());
                i++;
            }

        }

        private void dtGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void btnResest_Click(object sender, RoutedEventArgs e)
        {
            winResetValue winReset = new winResetValue(DataType.DOUBLE);
            winReset.Owner = this;
            if (winReset.ShowDialog().Value)
            {
                for (int i = 0; i < this._lstDouble.Count; i++)
                {
                    this._lstDouble[i] = Convert.ToDouble(winReset.txtResetValue.Text);
                    ShowData(this._lstDouble);
                }
            }
        }
    }
}
